package com.ruoyi.system.enservice.impl;

import com.ruoyi.system.domain.GaAlarm;
import com.ruoyi.system.domain.GaAlarmUpdateRecord;
import com.ruoyi.system.domain.GaUser;
import com.ruoyi.system.dto.input.AlarmDetailGetInput;
import com.ruoyi.system.dto.output.AlarmDetailOutput;
import com.ruoyi.system.enservice.IAlarmEnService;
import com.ruoyi.system.service.IGaAlarmService;
import com.ruoyi.system.service.IGaAlarmUpdateRecordService;
import com.ruoyi.system.service.IGaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.sql.Date;
import java.util.List;

@Service
public class AlarmEnServiceImpl implements IAlarmEnService {
    @Autowired
    private IGaAlarmService gaAlarmService;

    @Autowired
    private IGaUserService gaUserService;

    @Autowired
    private IGaAlarmUpdateRecordService gaAlarmUpdateRecordService;

    /**
     * 通过学生ID和案件ID查询该案件的详细信息
     * @author xuankai
     * @param dto 包含学生ID与案件ID的请求体
     * @return 案件的详细信息
     **/
    public AlarmDetailOutput getDetail(AlarmDetailGetInput dto) {
        GaUser user = gaUserService.selectGaUserById(dto.getStudentId());
        GaAlarm alarm = gaAlarmService.selectGaAlarmById(dto.getAlarmId());
        GaUser police = new GaUser();

        GaAlarmUpdateRecord updateRecord = new GaAlarmUpdateRecord();
        updateRecord.setAlarm(dto.getAlarmId());

        List<GaAlarmUpdateRecord> record = gaAlarmUpdateRecordService
                .selectGaAlarmUpdateRecordList(updateRecord);

        //有处理记录的情况
        if(!record.isEmpty()) {
            police = gaUserService.selectGaUserById(record.get(0).getUser());
        }

        return new AlarmDetailOutput()
                .setAlarmId(dto.getAlarmId())
                .setAlarmTitle(alarm.getTitle())
                .setAlarmType(alarm.getType())
                .setAlarmContent(alarm.getContent())
                .setAlarmCreateTime(alarm.getCreateTime())
                .setAlarmState(alarm.getState())
                .setStudentId(dto.getStudentId())
                .setStudentName(user.getName())
                .setStudentTel(user.getTel())
                .setPoliceId(police.getId())
                .setPoliceName(police.getName());
    }

    /**
     * @Description: 更新报警信息并写入报警信息更新记录表
     * @author ckyforever
     * @param gaAlarm 新的报警信息
     * @param userId 当前用户Id
     * @return boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editAlarm(GaAlarm gaAlarm,Long userId) {
        try{
            GaAlarmUpdateRecord record = new GaAlarmUpdateRecord();
            record.setAlarm(gaAlarm.getId());
            record.setOperateTime(new Date(System.currentTimeMillis()));
            record.setUser(userId);
            GaAlarm ga = gaAlarmService.selectGaAlarmById(gaAlarm.getId());
            String update = "";
            if(!ga.getState().equals(gaAlarm.getState())) {
                update += "更新报警状态为" + gaAlarm.getState() +"\n";
            }
            if(!ga.getContent().equals(gaAlarm.getContent())) {
                update += "更新报警内容为" + gaAlarm.getContent() + "\n";
            }
            if(!ga.getTitle().equals(gaAlarm.getTitle())) {
                update += "更新报警标题为" + gaAlarm.getTitle() + "\n";
            }
            record.setOperateContent(update);
            record.setOperateState("更新");
            record.setAlarm(gaAlarm.getId());
            gaAlarmUpdateRecordService.insertGaAlarmUpdateRecord(record);
            gaAlarmService.updateGaAlarm(gaAlarm);
        } catch (Exception e) {

            return false;
        }
        return true;
    }
}
